const log4js = require('log4js');
const log4jsConf = require('./log4jsConf')
const dateUtils = require('../../utils/dateUtils')
const {
	$_ULOG,
} = require('./logger')

module.exports = function() {
	log4js.configure(log4jsConf);
	// Console replacement
	// V1 used to allow you to replace the node.js console functions with versions that would log to a log4js appender. This used to cause some weird errors, so I decided it was better to remove it from the log4js core functionality. 
	const logger = log4js.getLogger('console');
	console.log = logger.info.bind(logger);
	return logMiddleWare;
}

function logMiddleWare(cxt, next) {
	$_ULOG.call(cxt, `---request日志开始: ${cxt.path}, 开始时间:${dateUtils.getTime()}---`)
	let info = {
		datetime: dateUtils.getMSTime(),
		api: cxt.originalUrl.split("?")[0],
		reqip: cxt.ip,
		reqips: cxt.ips,
		method: cxt.method,
		query: cxt.query,
		body: cxt.request.body,
		headers: cxt.headers,
		originalUrl: cxt.originalUrl,
	};
	$_ULOG.call(cxt, JSON.stringify(info))
	return next();
}